package com.lxw.ObserverPaterns.JavaObserver;

import com.lxw.ObserverPaterns.Display.DisplayElement;

import java.util.Observable;
import java.util.Observer;

public class CurrentConditionsDisplay implements Observer, DisplayElement {

    Observable observable;
    private double temperature;
    private double humidity;
    private double pressure;

    public CurrentConditionsDisplay(Observable o){
        this.observable=o;
        observable.addObserver(this);
    }
    public void display() {
        System.out.println("当前天气状态 温度："+temperature+" 湿度:"+humidity+" 压力:"+pressure );
    }

    public void update(Observable o, Object arg) {
        if(o instanceof  WeatherData){
            WeatherData weatherData=(WeatherData)o;
            this.temperature=weatherData.getTemperature();
            this.humidity=weatherData.getHumidity();
            this.pressure=weatherData.getPressure();
            display();
        }
    }
}
